local fk__yinbing = fk.CreateSkill {

  name = "fk__yinbing",

  tags = {  },

}

local U = require "packages/utility/utility"


fk__yinbing:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(fk__yinbing.name) and target == player and player.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    local tos = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player, false), Util.IdMapper), 1, 1, "#fk__yinbing-choose", fk__yinbing.name, true, true)
    if #tos > 0 then
      event:setCostData(self, tos[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local mark = player:getTableMark(fk__yinbing.name)
    table.insertIfNeed(mark, event:getCostData(self))
    player.room:setPlayerMark(player, fk__yinbing.name, mark)
  end,
})
fk__yinbing:addEffect(fk.TurnStart, {
  refresh_events = {fk.TurnStart},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark(fk__yinbing.name) ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, fk__yinbing.name, 0)
  end,
})

fk__yinbing:addEffect(fk.TargetConfirming, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetConfirming then
      return not target.dead and table.contains(player:getTableMark("fk__yinbing"), target.id) and (data.card.name == "duel" or data.card.trueName == "slash") and not table.contains(data:getAllTargets(), player.id) and not data.from:isProhibited(player, data.card)
    elseif data.extra_data and table.contains((data.extra_data.fk__yinbing or {}), player.id) then
      local from =data.from
      return player:canPindian(from)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetConfirming then
      player:broadcastSkillInvoke("fk__yinbing", 3)
      data:addTarget(player)
     data:cancelTarget(target)
      data.extra_data = data.extra_data or {}
      data.extra_data.fk__yinbing = data.extra_data.fk__yinbing or {}
      table.insertIfNeed(data.extra_data.fk__yinbing, player.id)
      return true
    elseif room:askForSkillInvoke(player, "fk__yinbing", nil, "#fk__yinbing-pindian:"..data.from.id) then
      player:pindian({data.from}, "fk__yinbing")
    end
  end,
})

fk__yinbing:addEffect(fk.TargetConfirmed, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if data.extra_data and table.contains((data.extra_data.fk__yinbing or {}), player.id) then
      local from = data.from
      return player:canPindian(from)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:askForSkillInvoke(player, "fk__yinbing", nil, "#fk__yinbing-pindian:"..data.from.id)
    player:pindian({data.from}, "fk__yinbing")
 
  end,
})

return fk__yinbing